package com.jic.market.common.util;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author yinhaijun
 * @date: 2020/4/8
 */
public class Money implements Serializable {
    private static final long serialVersionUID = -4112535988285797656L;

    /**
     * 加法
     * @param source
     * @param taget
     * @return
     */
    public static BigDecimal add(BigDecimal source, BigDecimal taget){
        if(null == source){
            source = BigDecimal.ZERO;
        }
        if(null == taget){
            taget = BigDecimal.ZERO;
        }
        return source.add(taget);
    }

    /**
     * 减法
     * @param source
     * @param taget
     * @return
     */
    public static BigDecimal subtract(BigDecimal source, BigDecimal taget){
        if(null == source){
            source = BigDecimal.ZERO;
        }
        if(null == taget){
            taget = BigDecimal.ZERO;
        }
        return source.subtract(taget);
    }

    /**
     * 除法
     * @param source
     * @param taget
     * @return
     */
    public static BigDecimal divide(BigDecimal source, BigDecimal taget){
        return source.divide(taget,2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 除法，取整
     * @param source
     * @param taget
     * @return
     */
    public static BigDecimal divideInt(BigDecimal source, BigDecimal taget){
        return source.divide(taget,0, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 乘法
     * @param source
     * @param taget
     * @return
     */
    public static BigDecimal multiply(BigDecimal source, BigDecimal taget){
        return source.multiply(taget).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
